package com.lottoad.android.cronos.parser;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.lottoad.android.cronos.data.CNLottoAdAdressData;
import com.lottoad.android.cronos.datahandler.CNBaseDataParser;
import com.lottoad.android.cronos.datahandler.CNResultSet;

public class CNZipcodListParser extends CNBaseDataParser{
	protected void onParseContent(Element xmlObject, CNResultSet resultSet)
			throws Exception {
		int count = 0;
		
		NodeList nodeList = null;
		NodeList nodeList1 = xmlObject.getElementsByTagName("sido");
		NodeList nodeList2 = xmlObject.getElementsByTagName("gugun");
		NodeList nodeList3 = xmlObject.getElementsByTagName("dong");
		
		int length1 = nodeList1.getLength();
		int length2 = nodeList2.getLength();
		int length3 = nodeList3.getLength();
		
		if(length1 != 0){
			count = length1;
			nodeList = nodeList1;
		}
		
		else if(length2 != 0){
			count = length2;
			nodeList = nodeList2;
		}
		
		else if(length3 != 0){
			count = length3;
			nodeList = nodeList3;
		}
		
		for (int i = 0; i < count; i++)
		{
			Element elementData = (Element) nodeList.item(i);
			if (elementData == null)
				continue;
			
			CNLottoAdAdressData dataSet = new CNLottoAdAdressData();
			
			if(nodeList1.getLength()>0){
				if(nodeList1.item(i).getFirstChild() != null)
					dataSet.adressSIDO = nodeList1.item(i).getFirstChild().getNodeValue();
			}
			
			if(nodeList2.getLength()>0){
				if(nodeList2.item(i).getFirstChild() != null)
					dataSet.adressGUGUN = nodeList2.item(i).getFirstChild().getNodeValue();
			}
			
			if(nodeList3.getLength()>0){
				if(nodeList3.item(i).getFirstChild() != null)
					dataSet.adressDONG = nodeList3.item(i).getFirstChild().getNodeValue();
			}
			
			resultSet.addDataSet(dataSet);
		}
	}
}
